<template>
  <div class="ChildPage TablePage" v-loading="loading">
    <vb-page ref="page">
      <template #headerSearch-prepend="scope">
        <el-button size="small" v-loading="materialLoading" @click="reloadMaterial? getMaterialReloadResult(true): doMaterialSync()" :icon="reloadMaterial? 'el-icon-loading': 'el-icon-refresh'">{{ reloadMaterial? '同步中，点击刷新': '同步物料' }}</el-button>
        <div style="flex-grow: 1; width: 100%;"></div>
      </template>
      <template #headerSearch-btn-append="{ searchForm }">
        <el-button type="warning" size="small" icon="el-icon-download" @click="doDownload(searchForm)">导出物料</el-button>

      </template>
      <template #table-unit="scope">
        {{ scope.row.fBaseUnitIdFName }}({{ scope.row.fBaseUnitIdNumber }})
      </template>
    </vb-page>
  </div>
</template>

<script>
export default {
  data() {
    return {
      materialLoading: false,
      reloadMaterial: false,
    }
  },
  methods: {
    async doDownload(searchForm) {
      this.loading = true
      try {
        let res = await this.callApi('download_material', searchForm)
        if (!res.res) return
      } finally {
        this.loading = false
      }
    },
    /**
     * 获取加载按钮的加载中状态
     */
    async getMaterialReloadResult(showMsg) {
      this.materialLoading = true
      try {
        let res = await this.callApi("get_material_reload_result")
        if (!res.res) return
        this.reloadMaterial = res.reloadMaterial
        this.reloadMaterialRes = res.reloadMaterialRes
        this.reloadMaterialMsg = res.reloadMaterialMsg
        this.reloadMaterialStartTime = res.reloadMaterialStartTime
        this.reloadMaterialEndTime = res.reloadMaterialEndTime
        if (showMsg) {
          if (this.reloadMaterial) {
            this.$message.info('物料数据同步中')
          } else {
            if (this.reloadMaterialRes) {
              this.reload()
              this.$message.success('已同步完成, 请刷新查看最新结果')
            } else {
              this.$message.error('同步失败，' + this.reloadMaterialMsg)
            }
          }
        }
      } finally {
        this.materialLoading = false
      }
    },
    /**
     * 同步物料
     * */
    async doMaterialSync() {
      try {
        let msg = '';
        if (this.reloadMaterialStartTime) {
          msg += `<span style="font-weight: bold;">同步物料，耗时较长，是否继续执行？</span><br />
          前次同步${ this.reloadMaterialRes? '成功!': `<span style="color: var(--danger)">失败!</span>` } ${ this.reloadMaterialEndTime }
          ${ this.reloadMaterialRes? '': `<br />${ this.reloadMaterialMsg }` }
          <br />`;
        } else {
          msg = '同步物料，耗时较长，是否继续执行？';
        }
        await this.$confirm(msg, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          dangerouslyUseHTMLString: true
        })
      } catch(e) {
        return
      }
      try {
        this.loading = true
        let res = await this.callApi("sync_material")
        if (!res.res) return
        this.reloadMaterial = true
        this.$message.success('已经开始同步')
      } finally {
        this.loading = false
      }
    },
  }
}
</script>

<style scoped>

</style>
